﻿using Dwgc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.GraphicsInterface;

namespace Dwg_output
{
    class Num_Tag
    {
        public ObjectId T_DO9011;
        public ObjectId T_DI9011;
        public ObjectId T_DI9001;
        public ObjectId T_AI9001;
        public ObjectId T_GT9001;
        public ObjectId B_IO9001_11;
        public int base_B_IO = 12;//数据库定位
        public int signal_power = 21;
        public int safe_relay = 19;
        public int safe_relay_num = 20;
        public int termail = 16;
        public int termail_board = 14;



        public ObjectId Wire_Two;
        public ObjectId Wire_3RTD;
        public ObjectId Wire_3AI;
        public ObjectId Duznazi;
        public ObjectId Duznazi12;
        public ObjectId Safe_Relay_Block;

    }



    class Wire_Dwg
    {

        private ObjectId T_DO9011;
        private ObjectId T_DI9011;
        private ObjectId T_DI9001;
        private ObjectId T_AI9001;
        private ObjectId T_GT9001;
        private ObjectId B_IO9001_11;

        private ObjectId T_AI3201;
        private ObjectId T_AI1601;
        private ObjectId T_AO1601;
        private ObjectId T_DI1601;
        private ObjectId T_DO1601;
        private ObjectId T_DO1602;

        public int base_B_IO = 12;//数据库定位
        public int signal_power = 21;
        public int safe_relay = 19;
        public int safe_relay_num = 20;
        public int termail = 16;
        public int configuration = 11;
        public int sign_type = 3;
        public int spd = 17;
        public int spd_num = 17;

        private int duanzi_big =12;
        private ObjectId Wire_Two;
        private ObjectId Wire_3RTD;
        private ObjectId Wire_3AI;
        private ObjectId Duznazi;
        private ObjectId Duznazi12;
        private ObjectId Safe_Relay_Block;


        public static void Tukuang_Dwg(List<string> projectValue, List<string> jigui_name, int[] x_pos, Database sourceDb, Database destDb)
        {
            try
            {
                ObjectId test = CopyAttributeBlock.CopyAttributeBlocks(sourceDb, destDb, "中文竖图框", "中文竖图框1");
                ObjectId covershet_name = CopyAttributeBlock.CopyAttributeBlocks(sourceDb, destDb, "COVERSHT", "COVERSHT1");
                using (Transaction tr = destDb.TransactionManager.StartTransaction())
                {
                    Point3d pt1 = new Point3d(0, 0, 0);
                    for (int i = 0; i < jigui_name.Count; i++)
                    {
                        //获取指定的块ID
                       
                        if (int.Parse(test.ToString()) == 0)
                        {
                            MessageBox.Show("图框块获取失败");
                            return;
                        }

                       // 封面

            
                   

                        BlockReference blkREF_COVER_NAME = CopyAttributeBlock.AddAttributeReference(destDb, covershet_name, pt1.Add(new Vector3d(-3100, (-4800) * i, 0)).Add(new Vector3d(1142, -2500, 0)), 0, 1);
                        CopyAttributeBlock.GetAttributeVal(destDb, blkREF_COVER_NAME, "COVERSHT1" );
                        CopyAttributeBlock.SetAttributeVal(destDb, blkREF_COVER_NAME, "机柜名称", jigui_name[i]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkREF_COVER_NAME, "图纸名称", "回路接线图纸");
                        CopyAttributeBlock.SetAttributeVal(destDb, blkREF_COVER_NAME, "工单号", projectValue[3]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkREF_COVER_NAME, "项目名称", projectValue[1]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkREF_COVER_NAME, "用户名称", projectValue[2]);






                        BlockReference blkRef1 = CopyAttributeBlock.AddAttributeReference(destDb, test, pt1.Add(new Vector3d(-3100, -4800 * i, 0)), 0, 1);
                        CopyAttributeBlock.GetAttributeVal(destDb, blkRef1, "中文竖图框1" );
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "日期", projectValue[11]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "设计", projectValue[6]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "审核", projectValue[7]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "审定", projectValue[8]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "版本", projectValue[12]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "项目名称", projectValue[1]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "用户名称", projectValue[2]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "项目编号", projectValue[3]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "描述", "用于集成");


                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "第几页", "第1页");
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "机柜名称", jigui_name[i]);
                        CopyAttributeBlock.SetAttributeVal(destDb, blkRef1, "图纸名称", "回路接线图");


                        //接线图框
                        for (int j = 0; j < x_pos[i]; j++)
                        {

                            BlockReference blkRef2 = CopyAttributeBlock.AddAttributeReference(destDb, test, pt1.Add(new Vector3d(3100 * j, -4800 * i, 0)), 0, 1);
                            CopyAttributeBlock.GetAttributeVal(destDb, blkRef2, "中文竖图框1" );
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "日期", projectValue[11]);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "设计", projectValue[6]);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "审核", projectValue[7]);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "审定", projectValue[8]);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "版本", projectValue[12]);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "项目名称", projectValue[1]);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "用户名称", projectValue[2]);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "项目编号", projectValue[3]);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "描述", "用于集成");
                            string page = "第" + (j + 2) + "页";

                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "第几页", page);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "机柜名称", jigui_name[i]);
                            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "图纸名称", "回路接线图");


                        }




                    }



                    tr.Commit();


                }

            }
            catch (System.Exception ex)
            {
                // 处理异常
                MessageBox.Show(ex.Message + "图框信息有误，请检查是否有空数据");
                Console.WriteLine("发生异常：" + ex.Message);
            }



        }

        private static void TagText(List<List<string>> tagtext, Point3d pt1, Database destDb, int point_y, int i)
        {
            // Point3d pt1 = new Point3d(330, -700, 0);
            int Tag_D_Space = 30;//一个位号的间距
            int Tag_S_Space = 104;//纯端子板DI AI AO 二线制信号间距
            int Tag_J_Space = 4800;//两个机柜的间距




            //for (int i = 0; i < tagtext.Count; i++)
            // {


            //         Creat_text_Dwg.ctrateText(tagtext[i][1], destDb
            //                         , pt1.Add(new Vector3d(0, -(Tag_J_Space * (point_y - 1)) - Tag_S_Space * i , 0)), 20, 0, 2);
            //         Creat_text_Dwg.ctrateText(tagtext[i][2], destDb
            //             , pt1.Add(new Vector3d(0, -(Tag_J_Space * (point_y - 1)) - Tag_D_Space - Tag_S_Space * i , 0)), 20, 0, 2);



            // }
            Creat_text_Dwg.ctrateText(tagtext[i][1], destDb
                                  , pt1.Add(new Vector3d(0, 0, 0)), 20, 0, 2);
            Creat_text_Dwg.ctrateText(tagtext[i][2], destDb
                       , pt1.Add(new Vector3d(0, -Tag_D_Space, 0)), 20, 0, 2);


        }//输出单个位号

        public int[] ALL_WriteToDwg(List<string> jiguiALL, int max_num, string filePath_Db, Database sourceDb, Database destDb)
        {

            int y_Pos = 0;
            int x_Pos = 0;
            int tag_16Biao = 0;
            int tag_32Biao = 0;
            int[] x_posBiao = new int[40];//最多40个机柜
            int tk_BIao = 0;    //图纸数量


            //Wire_Dwg BOARD = new Wire_Dwg();

            Dictionary<int, string> Dict_sys_jigui = new Dictionary<int, string>();
            Dict_sys_jigui = Wire_ReadSQL.Read_Sys_Jigui_Dictionary(filePath_Db, 1);

            Dictionary<int, string> Dict_jigui = new Dictionary<int, string>();
            Dict_jigui = Wire_ReadSQL.Read_Jigui_Dictionary(filePath_Db, 1);

            for (int i = 0; i < jiguiALL.Count; i++)//不同的机柜
            {

                x_Pos = 0;
                tk_BIao = 0;
                tag_16Biao = 0;
                int jigui_id = Wire_ReadSQL.ReadJigui_ID_FromSql(filePath_Db, jiguiALL[i]);//读取当前机柜ID

                Wire_Two = CopyBlock.CopyBlocks(sourceDb, destDb, "二线制位号_B", "二线制位号_B" + i.ToString());
                Wire_3RTD = CopyBlock.CopyBlocks(sourceDb, destDb, "三线制RTD_J", "三线制RTD_J" + i);
                Wire_3AI = CopyBlock.CopyBlocks(sourceDb, destDb, "三线制AI_J", "三线制AI_J" + i);
                Duznazi = CopyBlock.CopyBlocks(sourceDb, destDb, "替代端子", "替代端子" + i);
                Duznazi12 = CopyBlock.CopyBlocks(sourceDb, destDb, "一进二出端子替代块", "一进二出端子替代块" + i);


                for (int j = 0; j <= max_num; j++)//这个循环为遍历不同卡件的所有位号
                {


                    List<List<string>> tag = Wire_ReadSQL.ReadTagFromSql(filePath_Db, j, jigui_id);
                    if (tag.Count == 0)//获取到当前卡件内的位号为0时
                    {
                        continue;
                    }

                    if (tag_16Biao % 2 == 0)
                    {
                        y_Pos = 0;//第一个为16卡件

                    }
                    else
                    {

                        y_Pos = 1880;
                    }


                    if (tag.Count <= 16)//如果位号为8个或者16个时，坐标增量为半图纸
                    {

                        Point3d pt1_Tag = new Point3d(620 + (x_Pos) * 3100, -240 - (y_Pos), 0);//原点位号坐标
                                                                                               //  Wire_Dwg.TagText(tag, pt1_Tag, destDb, jigui_id);//添加位号
                        TagWireLine(destDb, pt1_Tag, tag, Wire_Two, Wire_3RTD, Wire_3AI, jigui_id);//位号引脚
                        Duanzi_toDwg(destDb, pt1_Tag, tag, Duznazi, Wire_3RTD, jigui_id);
                        Line_toDwg(destDb, pt1_Tag, tag, jigui_id);
                        Termail_Board(sourceDb, destDb, pt1_Tag, tag, jigui_id);
                        Wire_Board.Board_DB_Line(destDb, pt1_Tag, tag, Dict_sys_jigui, Dict_jigui, jigui_id);
                        Termail_Mark_toDwg(destDb, pt1_Tag, tag, jigui_id);


                        if (tag_16Biao % 2 == 0)
                        {

                            tk_BIao++;
                        }
                        else
                        {
                            x_Pos++;
                        }

                        tag_16Biao++;
                    }

                    else if (tag.Count > 16)//如果位号为8个或者16个时，坐标增量为半图纸
                    {

                        if (tag_16Biao % 2 == 1)
                        {

                            x_Pos++;
                            tag_16Biao = 0;
                        }



                        y_Pos = 0;//第一个为16卡件
                        Point3d pt1_Tag = new Point3d(620 + (x_Pos) * 3100, -240 - (y_Pos), 0);

                        Wire_Dwg.TagWireLine(destDb, pt1_Tag, tag, Wire_Two, Wire_3RTD, Wire_3AI, jigui_id);//位号引脚
                        Duanzi_toDwg(destDb, pt1_Tag, tag, Duznazi, Wire_3RTD, jigui_id);
                        Line_toDwg(destDb, pt1_Tag, tag, jigui_id);
                        Termail_Board(sourceDb, destDb, pt1_Tag, tag, jigui_id);
                        Wire_Board.Board_DB_Line(destDb, pt1_Tag, tag, Dict_sys_jigui, Dict_jigui, jigui_id);
                        Termail_Mark_toDwg(destDb, pt1_Tag, tag, jigui_id);


                        x_Pos++;
                        tk_BIao++;

                    }
                    x_posBiao[i] = tk_BIao;
                }

            }
            return x_posBiao;
        }//输出整个位号

        public List<string> Safe_Relay_ALL_WriteToDwg(List<string> jiguiALL, int max_num, string filePath_Db, Database sourceDb, Database destDb)
        {

            int y_Pos = 0;
            int x_Pos = 0;
            int tag_16Biao = 0;
            int tag_32Biao = 0;
            List<string> error_all = new List<string>();
            int tk_BIao = 0;    //图纸数量



            Wire_Dwg wire_Dwg = new Wire_Dwg();
            Dictionary<int, string> Dict_sys_jigui = new Dictionary<int, string>();
            Dict_sys_jigui = Wire_ReadSQL.Read_Sys_Jigui_Dictionary(filePath_Db, 1);

            Dictionary<int, string> Dict_jigui = new Dictionary<int, string>();
            Dict_jigui = Wire_ReadSQL.Read_Jigui_Dictionary(filePath_Db, 1);

            for (int i = 0; i < jiguiALL.Count; i++)//不同的机柜
            {

                x_Pos = 0;
                tk_BIao = 0;
                tag_16Biao = 0;
                int jigui_id = Wire_ReadSQL.ReadJigui_ID_FromSql(filePath_Db, jiguiALL[i]);//读取当前机柜ID


                for (int j = 0; j <= max_num; j++)//这个循环为遍历不同卡件的所有位号
                {


                    List<List<string>> tag = Wire_ReadSQL.ReadTagFromSql(filePath_Db, j, jigui_id);
                    if (tag.Count == 0)//获取到当前卡件内的位号为0时
                    {
                        continue;
                    }

                    if (tag_16Biao % 2 == 0)
                    {
                        y_Pos = 0;//第一个为16卡件

                    }
                    else
                    {

                        y_Pos = 1880;
                    }


                    if (tag.Count <= 16)//如果位号为8个或者16个时，坐标增量为半图纸
                    {


                        //*************************************************************************************************************//
                        Point3d pt1_Tag = new Point3d(620 + (x_Pos) * 3100, -240 - (y_Pos), 0);//原点位号坐标


                        List<string> error_Tagl = wire_Dwg.Safe_relay_Tag(sourceDb, destDb, pt1_Tag, tag, jigui_id);



                        error_all.AddRange(error_Tagl);
                        //*************************************************************************************************************// 


                        if (tag_16Biao % 2 == 0)
                        {

                            tk_BIao++;
                        }
                        else
                        {
                            x_Pos++;
                        }

                        tag_16Biao++;
                    }

                    else if (tag.Count > 16)//如果位号为8个或者16个时，坐标增量为半图纸
                    {


                        if (tag_16Biao % 2 == 1)
                        {

                            x_Pos++;
                            tag_16Biao = 0;
                        }



                        y_Pos = 0;//第一个为16卡件
                        Point3d pt1_Tag = new Point3d(620 + (x_Pos) * 3100, -240 - (y_Pos), 0);
                        //**************************************************************************************************//
                        List<string> error_Tagl = wire_Dwg.Safe_relay_Tag(sourceDb, destDb, pt1_Tag, tag, jigui_id);
                        error_all.AddRange(error_Tagl);



                        //**************************************************************************************************//
                        x_Pos++;
                        tk_BIao++;

                    }

                }

            }
            return error_all;
        }//输出整个位号

        private static void TagWireLine(Database destDb, Point3d pt11, List<List<string>> tagtext, ObjectId twoWire, ObjectId rtd3Wire, ObjectId Wire_3AI, int point_y)
        {
            using (Transaction tr = destDb.TransactionManager.StartTransaction())
            {
                int Tag_S_Space = 104;//纯端子板DI AI AO 二线制信号间距

                //Point3d pt1 = new Point3d(100, 0, 0);
                //Point3d pt2 = new Point3d(1000, 0, 0);
                //Point3d pt3 = new Point3d(2000, 0, 0);
                //Point3d pt4 = new Point3d(3000, 0, 0);

                for (int i = 0; i < tagtext.Count; i++)
                {
                    Point3d pt1 = pt11.Add((new Vector3d(0, -(4800 * (point_y - 1)) - Tag_S_Space * i, 0)));

                    if (tagtext[i][11].Equals("二线制") || tagtext[i][11].Equals("常开") || tagtext[i][11].Equals("常闭") || tagtext[i][11].Equals("四线制"))
                    {//获取指定的块ID

                        CopyBlock.AddReference(destDb, twoWire, pt1, 0, 1);
                        Wire_Dwg.TagText(tagtext, pt1, destDb, point_y, i);
                    }
                    else if (tagtext[i][11].Equals("三线制") && !tagtext[i][3].Equals("4-20mA"))
                    {
                        CopyBlock.AddReference(destDb, rtd3Wire, pt1.Add(new Vector3d(0, -13, 0)), 0, 1);
                        Wire_Dwg.TagText(tagtext, pt1.Add(new Vector3d(0, -13, 0)), destDb, point_y, i);
                    }
                    else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("4-20mA"))
                    {
                        CopyBlock.AddReference(destDb, Wire_3AI, pt1.Add(new Vector3d(0, 13, 0)), 0, 1);
                        Wire_Dwg.TagText(tagtext, pt1.Add(new Vector3d(0, 13, 0)), destDb, point_y, i);

                    }
                    else
                    {
                        Wire_Dwg.TagText(tagtext, pt1, destDb, point_y, i);
                    }


                }
                tr.Commit();


            }



        }
        private static ObjectId TagWireLine_copyblock(Database sourceDb, Database destDb, string WireLine1, string WireLine2, int Tag)
        {
            using (Transaction tr = destDb.TransactionManager.StartTransaction())
            {
                ObjectId Wire_model = ObjectId.Null;
                if (WireLine1.Equals("二线制"))
                {//获取指定的块ID
                    Wire_model = CopyBlock.CopyBlocks(sourceDb, destDb, "二线制位号_B", "二线制位号_B" + Tag);

                }
                else if (WireLine2.Equals("三线制") && WireLine2.Equals("RTD"))
                {
                    Wire_model = CopyBlock.CopyBlocks(sourceDb, destDb, "三线制RTD_J", "三线制RTD_J" + Tag);
                }


                tr.Commit();
                return Wire_model;


            }
        }
        private void Duanzi_toDwg(Database destDb, Point3d ptC, List<List<string>> tagtext, ObjectId duanzi1, ObjectId duanzi2, int point_y)//卡件为单位
        {
            using (Transaction tr = destDb.TransactionManager.StartTransaction())
            {
                int Tag_S_Space = 104;//纯端子板DI AI AO 二线制信号间距

                Point3d duanzi = ptC.Add(new Vector3d(68, 0, 0));
                //Point3d pt1 = new Point3d(100, 0, 0);
                //Point3d pt2 = new Point3d(1000, 0, 0);
                //Point3d pt3 = new Point3d(2000, 0, 0);
                //Point3d pt4 = new Point3d(3000, 0, 0);


                for (int i = 0; i < tagtext.Count; i++)
                {
                    Point3d pt1 = duanzi.Add((new Vector3d(0, -(4800 * (point_y - 1)) - Tag_S_Space * i, 0)));

                    if (tagtext[i][termail] == "")
                    {
                        continue;  //没有端子就返回
                    }

                    //******************
                    if (tagtext[i][11].Equals("四线制") || tagtext[i][11].Equals("二线制") || tagtext[i][11].Equals("常开") || tagtext[i][11].Equals("常闭"))
                    {//获取指定的块ID
                        CopyBlock.AddReference(destDb, duanzi1, pt1.Add(new Vector3d(0, 13, 0)), 0, 1);
                        CopyBlock.AddReference(destDb, duanzi1, pt1.Add(new Vector3d(0, -13, 0)), 0, 1);


                        // CopyBlock.AddReference(destDb, twoWire, pt1, 0, 1);
                    }

                   else if ( tagtext[i][11].Equals("常开") || tagtext[i][11].Equals("常闭"))
                    {//获取指定的块ID
                        CopyBlock.AddReference(destDb, duanzi1, pt1.Add(new Vector3d(0, 13, 0)), 0, 1);
                        CopyBlock.AddReference(destDb, duanzi1, pt1.Add(new Vector3d(0, -13, 0)), 0, 1);


                        // CopyBlock.AddReference(destDb, twoWire, pt1, 0, 1);
                    }

                    else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("RTD"))
                    {
                        CopyBlock.AddReference(destDb, duanzi1, pt1.Add(new Vector3d(0, 13, 0)), 0, 1);
                        CopyBlock.AddReference(destDb, duanzi1, pt1.Add(new Vector3d(0, -13, 0)), 0, 1);
                        CopyBlock.AddReference(destDb, duanzi1, pt1.Add(new Vector3d(0, -26-13, 0)), 0, 1);
                    }
                    else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("4-20mA"))
                    {
                        //  CopyBlock.AddReference(destDb, duanzi1, pt1, 0, 1);
                        CopyBlock.AddReference(destDb, duanzi1, pt1.Add(new Vector3d(0, 39, 0)), 0, 1);
                        CopyBlock.AddReference(destDb, duanzi1, pt1.Add(new Vector3d(0, 13, 0)), 0, 1);
                        CopyBlock.AddReference(destDb, Duznazi12, pt1.Add(new Vector3d(0, -13, 0)), 0, 1);
                    }


                }
                tr.Commit();


            }



        }

        private void Termail_Mark_toDwg(Database destDb, Point3d ptC, List<List<string>> tagtext, int point_y)
        {
            using (Transaction tr = destDb.TransactionManager.StartTransaction())
            {
                int Tag_S_Space = 104;//纯端子板DI AI AO 二线制信号间距
                int j = 0;
                Point3d duanzi = ptC;
                //Point3d pt1 = new Point3d(100, 0, 0);
                //Point3d pt2 = new Point3d(1000, 0, 0);
                //Point3d pt3 = new Point3d(2000, 0, 0);
                //Point3d pt4 = new Point3d(3000, 0, 0);
                int currrnt_num = 0;
                string current_str = "";
                for (int i = 0; i < tagtext.Count; i++)
                {
                    Point3d pt1 = duanzi.Add((new Vector3d(0, -(4800 * (point_y - 1)) - Tag_S_Space * i, 0)));
                    if (tagtext[i][termail].Equals(""))
                    {
                        continue;
                    }
                    /////////////////////////************************
                    if (tagtext[i][11].Equals("四线制") || tagtext[i][11].Equals("二线制") || tagtext[i][11].Equals("常开") || tagtext[i][11].Equals("常闭"))
                    {
                        if (tagtext[i][termail] != current_str)
                        {
                            Creat_text_Dwg.ctrateText(tagtext[i][termail], destDb, pt1.Add(new Vector3d(90, 28, 0)), 12, 0, 0);
                            currrnt_num = 0;
                          
                        }
                        Creat_text_Dwg.ctrateText((currrnt_num + 1).ToString(), destDb, pt1.Add(new Vector3d(176, 4.5, 0)), 12, 0, 0);
                        Creat_text_Dwg.ctrateText((currrnt_num + 2).ToString(), destDb, pt1.Add(new Vector3d(176, 4.5 - 26, 0)), 12, 0, 0);
                        currrnt_num += 2 ;
                       
                      
                    }

                   else if ( tagtext[i][11].Equals("常开") || tagtext[i][11].Equals("常闭"))
                    {
                        if (tagtext[i][termail] != current_str)
                        {
                            Creat_text_Dwg.ctrateText(tagtext[i][termail], destDb, pt1.Add(new Vector3d(90, 28, 0)), 12, 0, 0);
                            currrnt_num = 0;
                        }
                        Creat_text_Dwg.ctrateText((currrnt_num + 1).ToString(), destDb, pt1.Add(new Vector3d(176, 4.5, 0)), 12, 0, 0);
                        Creat_text_Dwg.ctrateText((currrnt_num + 2).ToString(), destDb, pt1.Add(new Vector3d(176, 4.5 - 26, 0)), 12, 0, 0);
                        currrnt_num += 2;
                    }


                    else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("RTD"))
                    {

                        if (tagtext[i][termail] != current_str)
                        {
                            Creat_text_Dwg.ctrateText(tagtext[i][termail], destDb, pt1.Add(new Vector3d(90, 48.5, 0)), 12, 0, 0);
                            currrnt_num = 0;
                        }
                        Creat_text_Dwg.ctrateText((currrnt_num + 1).ToString(), destDb, pt1.Add(new Vector3d(176,  4.5, 0)), 12, 0, 0);
                        Creat_text_Dwg.ctrateText((currrnt_num + 2).ToString(), destDb, pt1.Add(new Vector3d(176,  4.5 - 26, 0)), 12, 0, 0);
                        Creat_text_Dwg.ctrateText((currrnt_num + 3).ToString(), destDb, pt1.Add(new Vector3d(176,  4.5 - 26 - 26, 0)), 12, 0, 0);
                        currrnt_num += 3 ;
                      
                      

                    }
                    else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("4-20mA"))
                    {
                        if (tagtext[i][termail] != current_str)
                        {
                            Creat_text_Dwg.ctrateText(tagtext[i][termail], destDb, pt1.Add(new Vector3d(90, 48.5, 0)), 12, 0, 0);
                            currrnt_num = 0;
                        }
                        Creat_text_Dwg.ctrateText((currrnt_num + 1).ToString(), destDb, pt1.Add(new Vector3d(176,26+4.5 , 0)), 12, 0, 0);
                        Creat_text_Dwg.ctrateText((currrnt_num + 2).ToString(), destDb, pt1.Add(new Vector3d(176, 26+4.5 - 26, 0)), 12, 0, 0);
                        Creat_text_Dwg.ctrateText((currrnt_num + 3).ToString(), destDb, pt1.Add(new Vector3d(176, 26+4.5 - 26-26 , 0)), 12, 0, 0);
                        Creat_text_Dwg.ctrateText((currrnt_num + 3).ToString(), destDb, pt1.Add(new Vector3d(176, 26+4.5 - 26 - 26-17, 0)), 12, 0, 0);
                        currrnt_num += 3;

                    }
                    current_str = tagtext[i][termail];


                }

                tr.Commit();

            }
        }


        private void Line_toDwg(Database destDb, Point3d ptC, List<List<string>> tagtext, int point_y)
            {   
                using (Transaction tr = destDb.TransactionManager.StartTransaction())
                {
                    int Tag_S_Space = 104;//纯端子板DI AI AO 二线制信号间距
                    int Safe_L = 300;
                    Point3d duanzi = ptC.Add(new Vector3d(68, 0, 0));
                    int Long_duanzi = 0;
                    int j = 0;
                Read_CXDatebase read_CXDatebase = new Read_CXDatebase();//返回错误

                for (int i = 0; i < tagtext.Count; i++)
                    {
                        Point3d pt1 = duanzi.Add((new Vector3d(0, -(4800 * (point_y - 1)) - Tag_S_Space * i, 0)));

                        Point3d Line_Right = pt1.Add(new Vector3d(1500, 0, 0));

                        if (tagtext[i][termail].Equals(""))//没有安全栅时
                            {
                                Long_duanzi = 0;
                            }
                        else
                            {
                                Long_duanzi = 125;
                            }

                        if (tagtext[i][safe_relay].Equals("")   )//没有安全栅时
                        {
                            if (tagtext[i][11].Equals("二线制") || tagtext[i][11].Equals("常开") || tagtext[i][11].Equals("常闭") || tagtext[i][11].Equals("四线制") || tagtext[i][11].Equals(""))
                            {
                                
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(1500, 13, 0)), destDb, 0);
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(1500, -13, 0)), destDb, 0);


                                Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);



                            }
                            else if (tagtext[i][11].Equals("三线制") &&    !    tagtext[i][3].Equals("4-20mA"))//热电阻
                            {
                                if (j == 0)
                                {
                                    j = i;
                                }

                           

                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(1500, 13, 0)), destDb, 0);
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(1500, -13, 0)), destDb, 0);
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13 - 26, 0)), pt1.Add(new Vector3d(1500, -13 - 26, 0)), destDb, 0);
                                Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);
                                j = j + 2;



                            }
                            else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("4-20mA"))
                            {
                                Long_duanzi = 125;
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(1500, 13, 0)), destDb, 0);
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(1500, -13, 0)), destDb, 0);
                                Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);
                                Power_signal.Power_signal_3wire(tagtext, destDb, pt1, i, i);
                            }
                        }
                    else if (string.IsNullOrEmpty(tagtext[i][spd]) && (read_CXDatebase.Read_equip_type(tagtext[i]).Equals("SPD") || tagtext[i][safe_relay_num].Contains("SPD")))
                    {

                        if (tagtext[i][11].Equals("二线制") || tagtext[i][11].Equals("常开") || tagtext[i][11].Equals("常闭") || tagtext[i][11].Equals("四线制") || tagtext[i][11].Equals(""))
                        {

                            Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(800, 13, 0)), destDb, 0);//安全栅前
                            Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(800, -13, 0)), destDb, 0);

                            Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, 13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, 13, 0)), destDb, 0);//安全栅后
                            Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, -13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, -13, 0)), destDb, 0);


                            Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);



                        }
                        else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("RTD"))//热电阻
                        {
                            if (j == 0)
                            {
                                j = i;
                            }

                            Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(800, 13, 0)), destDb, 0);//安全栅前
                                                                                                                                               // Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(800, -13, 0)), destDb, 0);

                            Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, 13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, 13, 0)), destDb, 0);//安全栅后
                            Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, -13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, -13, 0)), destDb, 0);


                            Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);
                            j = j + 2;

                        }
                        else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("4-20mA"))
                        {
                            Long_duanzi = 125;
                            Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(800, 13, 0)), destDb, 0);
                            Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(800, -13, 0)), destDb, 0);
                            Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);
                            Power_signal.Power_signal_3wire(tagtext, destDb, pt1, i, i);
                        }

                    }

                        else
                        {
                            if (tagtext[i][11].Equals("二线制") || tagtext[i][11].Equals("四线制") || tagtext[i][11].Equals("") || (tagtext[i][3].Equals("DO"))&&(tagtext[i][11].Equals("常开")) && tagtext[i][sign_type].Equals("干接点"))
                            {
                              
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(800, 13, 0)), destDb, 0);//安全栅前
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(800, -13, 0)), destDb, 0);

                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, 13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, 13, 0)), destDb, 0);//安全栅后
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, -13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, -13, 0)), destDb, 0);
                                Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);

                            }
                            else if ((tagtext[i][11].Equals("常开")) && (tagtext[i][sign_type].Equals("湿接点")|| tagtext[i][sign_type].Equals("干接点")))
                            {
                                Long_duanzi = 125;
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(800, 13, 0)), destDb, 0);//安全栅前
                                // Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(800, -13, 0)), destDb, 0);

                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, 13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, 13, 0)), destDb, 0);//安全栅后
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, -13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, -13, 0)), destDb, 0);
                                Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);

                            }

                            else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("RTD"))
                            {

                                if (j == 0)
                                {
                                    j = i;
                                }

                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(800, 13, 0)), destDb, 0);
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(800, -13, 0)), destDb, 0);
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13 - 26, 0)), pt1.Add(new Vector3d(800, -13 - 26, 0)), destDb, 0);

                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, 13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, 13, 0)), destDb, 0);//安全栅后
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(800 + Safe_L, -13, 0)), pt1.Add(new Vector3d(800 + Safe_L + 402.09, -13, 0)), destDb, 0);


                                 Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);
                                j = j + 2;

                            }
                            else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("4-20mA"))  //还没有考虑加三线制安全栅
                            {

                                Long_duanzi = 125;
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, 13, 0)), pt1.Add(new Vector3d(800, 13, 0)), destDb, 0);
                                Creat_Line_Dwg.Print_line(pt1.Add(new Vector3d(Long_duanzi, -13, 0)), pt1.Add(new Vector3d(800, -13, 0)), destDb, 0);
                                Wire_Board.Board_NumTag(destDb, Line_Right, tagtext, i, j);
                                Power_signal.Power_signal_3wire(tagtext, destDb, pt1, i, i);

                            }

                        }


                    }
                    tr.Commit();


                }


            }
        private List<string> Safe_relay_Tag(Database sourceDb, Database destDb, Point3d ptC, List<List<string>> tagtext, int point_y)
            {

                //16个
                using (Transaction tr = destDb.TransactionManager.StartTransaction())
                {
                    int Tag_S_Space = 104;//纯端子板DI AI AO 二线制信号间距
                    int Tag_L = 68;
                    int Safe_L = 300;
                    int Termial_L = 125;
                    int Long_duanzi = 0;
                    int j = 0;
                    bool Dou_safe = false;
                    string Safe_NUM = "";


                    Point3d duanzi = ptC.Add(new Vector3d(Tag_L, 0, 0));//端子定位位置


                    Read_CXDatebase read_CXDatebase = new Read_CXDatebase();//返回错误
                    List<string> error_list = new List<string>();



                    for (int i = 0; i < tagtext.Count; i++)
                    {
                        Point3d pt1 = duanzi.Add((new Vector3d(0, -(4800 * (point_y - 1)) - Tag_S_Space * i, 0)));

                        if (!tagtext[i][safe_relay].Equals(""))
                        {
                          
                             if (  i!= 0  &&  tagtext[i][safe_relay_num] == tagtext[i - 1][safe_relay_num])
                             { 
                               Dou_safe = true;
                             }


                             //模拟量安全栅
                            if (tagtext[i][11].Equals("二线制") || tagtext[i][11].Equals("四线制") || tagtext[i][11].Equals("") || (tagtext[i][11].Equals("常开") || tagtext[i][11].Equals("常闭")) && tagtext[i][sign_type].Equals("干接点"))
                                {

                                //插入引脚
                                string error_text = read_CXDatebase.SafeEquip(tagtext[i], destDb, pt1.Add(new Vector3d(800 + 4, 13 - 9, 0)), Dou_safe);
                                if (error_text != null)
                                {
                                    error_list.Add(error_text);
                                }
                                //插入快
                                Safe_relay_Block_INS(sourceDb, destDb, pt1.Add(new Vector3d(800, 13, 0)), tagtext, i);

                                 if (read_CXDatebase.Read_equip_type(tagtext[i]).Equals(""))
                                 {
                                   j = Power_signal.Power_signal_Safe(tagtext, destDb, pt1.Add(new Vector3d(800 + Safe_L, 13 - 52, 0)), j, i);
                                 }
                                    else if (read_CXDatebase.Read_equip_type(tagtext[i]).Equals("RY"))
                                    {
                                        j = Power_signal.Power_signal_Relay(tagtext, destDb, pt1.Add(new Vector3d(125, -13, 0)), j, i);
                                    }


                        }
                            //继电器
                            else if ((tagtext[i][11].Equals("常开") && tagtext[i][sign_type].Equals("湿接点")))
                            {
                                string error_text = read_CXDatebase.SafeEquip(tagtext[i], destDb, pt1.Add(new Vector3d(800 + 4, 13 - 9, 0)), Dou_safe);
                                if (error_text != null)
                                {
                                    error_list.Add(error_text);
                                }
                                Safe_relay_Block_INS(sourceDb, destDb, pt1.Add(new Vector3d(800, 13, 0)), tagtext, i);



                                if (read_CXDatebase.Read_equip_type(tagtext[i])=="")
                                {
                                   j = Power_signal.Power_signal_Safe(tagtext, destDb, pt1.Add(new Vector3d(800 + Safe_L, 13 - 52, 0)), j, i);

                                 }
                                else if (read_CXDatebase.Read_equip_type(tagtext[i]).Equals("RY"))
                                {
                                    j = Power_signal.Power_signal_Relay(tagtext, destDb, pt1.Add(new Vector3d(125, -13, 0)), j, i);
                                }
                          
                              
                            }

                            else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("RTD"))
                            {

                                //插入引脚
                                string error_text = read_CXDatebase.SafeEquip(tagtext[i], destDb, pt1.Add(new Vector3d(800 + 4, 13 - 9, 0)), Dou_safe);
                                if (error_text != null)
                                {
                                    error_list.Add(error_text);
                                }
                                //插入快
                                Safe_relay_Block_INS(sourceDb, destDb, pt1.Add(new Vector3d(800, 13, 0)), tagtext, i);
                                j= Power_signal.Power_signal_Safe(tagtext, destDb, pt1.Add(new Vector3d(800 + Safe_L, 13 - 52, 0)), j, i);
 
                            }
                            else if (tagtext[i][11].Equals("三线制") && tagtext[i][3].Equals("4-20mA"))
                            {

                                //插入引脚
                                string error_text = read_CXDatebase.SafeEquip(tagtext[i], destDb, pt1.Add(new Vector3d(800 + 4, 13 - 9, 0)), Dou_safe);
                                if (error_text != null)
                                {
                                    error_list.Add(error_text);
                                }
                                //插入快
                                Safe_relay_Block_INS(sourceDb, destDb, pt1.Add(new Vector3d(800, 13, 0)), tagtext, i);
                                j = Power_signal.Power_signal_Safe(tagtext, destDb, pt1.Add(new Vector3d(800 + Safe_L, 13 - 52, 0)), j, i);

                            }
                        Dou_safe = false;//复原


                        }


                    }
                    tr.Commit();

                    return error_list;
                }


            }
        public void Termail_Board(Database SourceDb, Database destDb, Point3d ptC, List<List<string>> tagtext, int point_y)
            {
            if (tagtext.Count<=2)
            {
                return;
            }
           
                Point3d line_right = ptC.Add(new Vector3d(68 + 1500, 0, 0));

                if (tagtext[1][14].Equals("T-DO9011"))
                {
                    if (int.Parse(T_DO9011.ToString()) == 0)
                    {
                        T_DO9011 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-DO9011_J", "T-DO9011_J");
                    }
                    Wire_Board.Board_DO9011(T_DO9011, destDb, line_right, tagtext, point_y);

                }

                else if (tagtext[1][14].Equals("T-DI9011"))
                {
                    if (int.Parse(T_DI9011.ToString()) == 0)
                    {
                        T_DI9011 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-DI9011_J", "T-DI9011_J");
                    }
                    Wire_Board.Board_DI9011(T_DI9011, destDb, line_right, tagtext, point_y);
                }

                else if (tagtext[1][14].Equals("T-DI9001"))
                {
                    if (int.Parse(T_DI9001.ToString()) == 0)
                    {
                        T_DI9001 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-DI9001_J", "T-DI9001_J");
                    }
                    Wire_Board.Board_DI9001(T_DI9001, destDb, line_right, tagtext, point_y);
                }

                else if (tagtext[1][14].Equals("T-AI9001"))
                {
                    if (int.Parse(T_AI9001.ToString()) == 0)
                    {
                        T_AI9001 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-AI9001_J", "T-AI9001_J");
                    }
                    Wire_Board.Board_AI9001(T_AI9001, destDb, line_right, tagtext, point_y);
                }
                else if (tagtext[1][14].Equals("T-GT9001"))
                {
                    if (int.Parse(T_GT9001.ToString()) == 0)
                    {
                        T_GT9001 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-GT9001_J", "T-GT9001_J");
                    }
                    Wire_Board.Board_GT9001(T_GT9001, destDb, line_right, tagtext, point_y);
                }

                else if (tagtext[1][base_B_IO].Equals("B-IO9001") || tagtext[1][base_B_IO].Equals("B-IO9011"))
                {
                    if (int.Parse(B_IO9001_11.ToString()) == 0)
                    {
                        B_IO9001_11 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "转接端子基座通用块", "转接端子基座通用块");
                    }
                    Wire_Board.Board_BI9001_11(B_IO9001_11, destDb, line_right, tagtext, point_y);


                }

          //  SIS

            else if (tagtext[1][14].Equals("T-AI3201"))
            {
                if (int.Parse(T_AI3201.ToString()) == 0)
                {
                    T_AI3201 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-AI3201_J", "T-AI3201_J");
                }
                Wire_Board.Board_AI3201(T_AI3201, destDb, line_right, tagtext, point_y);


            }
            else if (tagtext[1][14].Equals("T-DI1601"))
            {
                if (int.Parse(T_DI1601.ToString()) == 0)
                {
                    T_DI1601 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-DI1601_J", "T-DI1601_J");
                }
                Wire_Board.Board_DI1601(T_DI1601, destDb, line_right, tagtext, point_y);


            }
            else if (tagtext[1][14].Equals("T-DO1601"))
            {
                if (int.Parse(T_DO1601.ToString()) == 0)
                {
                    T_DO1601 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-DO1601_J2", "T-DO1601_J2");
                }
                Wire_Board.Board_DO1601(T_DO1601, destDb, line_right, tagtext[0], point_y,0);
               
            }
            else if (tagtext[1][14].Equals("T-DO1602"))
            {
                if (int.Parse(T_DO1602.ToString()) == 0)
                {
                    T_DO1602 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-DO1602_J2", "T-DO1602_J2");
                }
                Wire_Board.Board_DO1602(T_DO1602, destDb, line_right, tagtext[0], point_y,0);


            }
            else if (tagtext[1][14].Equals("T-AI1601"))
            {
                if (int.Parse(T_AI1601.ToString()) == 0)
                {
                    T_AI1601 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-AI1601_J2", "T-AI1601_J2");
                }
                Wire_Board.Board_AI1601(T_AI1601, destDb, line_right, tagtext, point_y);


            }
            else if (tagtext[1][14].Equals("T-AO1601"))
            {
                if (int.Parse(T_AO1601.ToString()) == 0)
                {
                    T_AO1601 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-AO1601_J", "T-AO1601_J");
                }
                Wire_Board.Board_AO1601(T_AO1601, destDb, line_right, tagtext, point_y);


            }

            if (tagtext.Count<=16)
            {
                return;
            }

            if (!string.IsNullOrEmpty(tagtext[16][14]) )
            {
              
                 if (tagtext[16][14].Equals("T-DI1601"))
                {
                    if (int.Parse(T_DI1601.ToString()) == 0)
                    {
                        T_DI1601 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-DI1601_J", "T-DI1601_J");
                    }
                    Wire_Board.Board_DI1601(T_DI1601, destDb, line_right, tagtext, point_y);


                }
                else if (tagtext[16][14].Equals("T-DO1601"))
                {
                    if (int.Parse(T_DO1601.ToString()) == 0)
                    {
                        T_DO1601 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-DO1601_J2", "T-DO1601_J2");
                    }
                    Wire_Board.Board_DO1601(T_DO1601, destDb, line_right, tagtext[16], point_y,1664);

                }
                else if (tagtext[16][14].Equals("T-DO1602"))
                {
                    if (int.Parse(T_DO1602.ToString()) == 0)
                    {
                        T_DO1602 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-DO1602_J2", "T-DO1602_J2");
                    }
                    Wire_Board.Board_DO1602(T_DO1602, destDb, line_right, tagtext[16], point_y,1664);


                }
                else if (tagtext[16][14].Equals("T-AI1601"))
                {
                    if (int.Parse(T_AI1601.ToString()) == 0)
                    {
                        T_AI1601 = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "T-AI1601_J2", "T-AI1601_J2");
                    }
                    Wire_Board.Board_AI1601(T_AI1601, destDb, line_right, tagtext, point_y);


                }


            }




        }
        public void Safe_relay_Block_INS(Database SourceDb, Database destDb, Point3d ptC, List<List<string>> tagtext, int i)
            {


                if (int.Parse(Safe_Relay_Block.ToString()) == 0)
                {
                    Safe_Relay_Block = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, "安全栅单通道接线块", "安全栅单通道接线块");
                }
                BlockReference blkRef2 = CopyAttributeBlock.AddAttributeReference(destDb, (ObjectId)Safe_Relay_Block, ptC, 0, 1);
                CopyAttributeBlock.GetAttributeVal(destDb, blkRef2, "安全栅单通道接线块");
                CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "安全栅编号", tagtext[i][safe_relay_num]);
                CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, "安全栅名称", tagtext[i][safe_relay]);
                blkRef2.Dispose();




            }

     }

    class Wire_short_power
    {
        public static  void Short_circuiting_3wire(Database destDb, Point3d ptC)
        {
            Creat_Line_Dwg.Print_line(ptC, ptC.Add(new Vector3d(15, -8.5, 0)), destDb, 0);//负极
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(15, -8.5, 0)), ptC.Add(new Vector3d(15, -8.5 - 87, 0)), destDb, 0);
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(15, -8.5 - 87, 0)), ptC.Add(new Vector3d(0, -8.5 - 87 - 8.5, 0)), destDb, 0);


            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(0, 69, 0)), ptC.Add(new Vector3d(30, -8.5 + 69, 0)), destDb, 0);
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(30, -8.5 + 69, 0)), ptC.Add(new Vector3d(30, -8.5 + 69 - 87, 0)), destDb, 0);
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(30, -8.5 + 69 - 87, 0)), ptC.Add(new Vector3d(0, -8.5 + 69 - 87 - 8.5, 0)), destDb, 0);


        }

        public static void Short_circuiting_Safe(Database destDb, Point3d ptC)
        {

            Creat_Line_Dwg.Print_line(ptC, ptC.Add(new Vector3d(25, -13, 0)), destDb, 0);//负极
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(25, -13, 0)), ptC.Add(new Vector3d(25, -13-78, 0)), destDb, 0);
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(25, -13-78, 0)), ptC.Add(new Vector3d(0, -13 - 78 - 13, 0)), destDb, 0);


            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(0, -26, 0)), ptC.Add(new Vector3d(15, -26-13, 0)), destDb, 0);
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(15, -26 -13, 0)), ptC.Add(new Vector3d(15, -26-13-78 , 0)), destDb, 0);
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(15, -26-13-78, 0)), ptC.Add(new Vector3d(0, -26-13-78-13, 0)), destDb, 0);


        }
        public static void Short_circuiting_Relay(Database destDb, Point3d ptC)
        {

            Creat_Line_Dwg.Print_line(ptC, ptC.Add(new Vector3d(25, -13, 0)), destDb, 0);//负极
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(25, -13, 0)), ptC.Add(new Vector3d(25, -13 - 78, 0)), destDb, 0);
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(25, -13 - 78, 0)), ptC.Add(new Vector3d(0, -13 - 78 - 13, 0)), destDb, 0);


            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(675, 0, 0)), ptC.Add(new Vector3d(675-25,  - 13, 0)), destDb, 0);
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(675 - 25, -13 , 0)), ptC.Add(new Vector3d(675 - 25,  - 13-78, 0)), destDb, 0);
            Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(675 - 25, - 13 - 78, 0)), ptC.Add(new Vector3d(675 ,  - 13 - 78-13, 0)), destDb, 0);


        }


    }

    class Power_signal
    {
    
        public static void Power_signal_3wire(List<List<string>> tagtext, Database destDb, Point3d ptC, int point_y, int i)
        {
            Num_Tag num_Tag = new Num_Tag();
            string[] power_Device = Wire_Board.Split_String(tagtext[i][num_Tag.signal_power]);



            if (power_Device[0].Equals(""))
            {

                // Short_circuiting(destDb, ptC.Add(new Vector3d(125, -30+104, 0)));
                // Short_circuiting(destDb, ptC.Add(new Vector3d(125, -30 , 0)));
                Wire_short_power.Short_circuiting_3wire(destDb, ptC.Add(new Vector3d(125, -30, 0)));

            }
            else
            {



                Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(125, -30, 0)), ptC.Add(new Vector3d(225, -30, 0)), destDb, 0);//负
                Creat_text_Dwg.ctrateText(power_Device[1], destDb, ptC.Add(new Vector3d(125, -30, 0)), 15, 0, 0);

                Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(125, -30 + 69, 0)), ptC.Add(new Vector3d(225, -30 + 69, 0)), destDb, 0);//正
                Creat_text_Dwg.ctrateText(power_Device[0], destDb, ptC.Add(new Vector3d(125, -30 + 69, 0)), 15, 0, 0);
                //如果下一个还是三线制就增加一个短接
                if (i != tagtext.Count - 1)
                {

                    Wire_short_power.Short_circuiting_3wire(destDb, ptC.Add(new Vector3d(125, -30, 0)));
                }


            }
      
        }

        public static int Power_signal_Safe(List<List<string>> tagtext, Database destDb, Point3d ptC, int j, int i)
        {
            Num_Tag num_Tag = new Num_Tag();

            string[] power_Device = Wire_Board.Split_String(tagtext[i][num_Tag.signal_power]);

            if (!power_Device[0].Equals(""))//有供电引脚时，写出这个引脚
            {

                Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(0, 0, 0)), ptC.Add(new Vector3d(100, 0, 0)), destDb, 0);//正
                Creat_text_Dwg.ctrateText(power_Device[0], destDb, ptC.Add(new Vector3d(0, 0, 0)), 15, 0, 0);

                Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(0, -26, 0)), ptC.Add(new Vector3d(100, -26, 0)), destDb, 0);//负
                Creat_text_Dwg.ctrateText(power_Device[1], destDb, ptC.Add(new Vector3d(0, -26, 0)), 15, 0, 0);
                j++;


            }


            if (((i+1)< tagtext.Count) )//在没到最后一个之前
            {
                string[] power_Device_n1 = Wire_Board.Split_String(tagtext[i+1][num_Tag.signal_power]);
                if ( !tagtext[i+1][num_Tag.safe_relay].Equals("")  &&(j%2==1)       )//下一个没有供电，就画出短截线
                {

                    // Short_circuiting(destDb, ptC.Add(new Vector3d(125, -30+104, 0)));
                    // Short_circuiting(destDb, ptC.Add(new Vector3d(125, -30 , 0)));
                    Wire_short_power.Short_circuiting_Safe(destDb, ptC.Add(new Vector3d(0, 0, 0)));



                }

                //if (tagtext[i + 1][num_Tag.safe_relay].Equals(""))//如果懒了怎么办
                //{

                //    Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(0, 0, 0)), ptC.Add(new Vector3d(100, 0, 0)), destDb, 0);//正
                //    Creat_text_Dwg.ctrateText(power_Device_n1[0], destDb, ptC.Add(new Vector3d(0, 0, 0)), 15, 0, 0);

                //    Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(0, -26, 0)), ptC.Add(new Vector3d(100, -26, 0)), destDb, 0);//负
                //    Creat_text_Dwg.ctrateText(power_Device_n1[1], destDb, ptC.Add(new Vector3d(0, -26, 0)), 15, 0, 0);

                //}
            }

            return j;
        }

        public static int Power_signal_Relay(List<List<string>> tagtext, Database destDb, Point3d ptC, int j, int i)
        {
            Num_Tag num_Tag = new Num_Tag();

            string[] power_Device = Wire_Board.Split_String(tagtext[i][num_Tag.signal_power]);

            if (!power_Device[0].Equals(""))//有供电引脚时，写出这个引脚
            {

                Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(0,0, 0)), ptC.Add(new Vector3d(100,0, 0)), destDb, 0);//负
                Creat_text_Dwg.ctrateText(power_Device[1], destDb, ptC.Add(new Vector3d(0, 0, 0)), 15, 0, 0);

                Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(575, 0, 0)), ptC.Add(new Vector3d(675, 0, 0)), destDb, 0);//正
                Creat_text_Dwg.ctrateText(power_Device[0], destDb, ptC.Add(new Vector3d(575, 0, 0)), 15, 0, 0);
                j++;

            }


            if (((i + 1) < tagtext.Count))//在没到最后一个之前
            {
                string[] power_Device_n1 = Wire_Board.Split_String(tagtext[i + 1][num_Tag.signal_power]);
                if (!tagtext[i + 1][num_Tag.safe_relay].Equals("")   &&   (j % 2 == 1)  )//下一个没有供电，就画出短截线
                {

                    // Short_circuiting(destDb, ptC.Add(new Vector3d(125, -30+104, 0)));
                    // Short_circuiting(destDb, ptC.Add(new Vector3d(125, -30 , 0)));
                    Wire_short_power.Short_circuiting_Relay(destDb, ptC.Add(new Vector3d(0, 0, 0)));

                }

                //if (tagtext[i + 1][num_Tag.safe_relay].Equals(""))//如果懒了怎么办
                //{

                //    Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(0, 0, 0)), ptC.Add(new Vector3d(100, 0, 0)), destDb, 0);//负
                //    Creat_text_Dwg.ctrateText(power_Device[1], destDb, ptC.Add(new Vector3d(0, 0, 0)), 15, 0, 0);

                //    Creat_Line_Dwg.Print_line(ptC.Add(new Vector3d(575, 0, 0)), ptC.Add(new Vector3d(675, 0, 0)), destDb, 0);//正
                //    Creat_text_Dwg.ctrateText(power_Device[0], destDb, ptC.Add(new Vector3d(575, 0, 0)), 15, 0, 0);



                //}
            }
            return j;

        }

    }

}
